<template>
  <div class="app-container dict-underLineReceivables-list-wrap">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="100px">

      <el-form-item label="ID" prop="code">
        <el-input v-model="queryParams.code" placeholder="" clearable />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="" clearable />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择" clearable>
          <el-option v-for="dict in dict.type.state" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="资金来源" prop="sourceOfFunds">
        <el-select v-model="queryParams.sourceOfFunds" placeholder="请选择" clearable>
          <el-option v-for="dict in dict.type.source_of_funds" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="适用分公司" prop="deptId">
        <treeselect v-model="queryParams.deptId" :options="deptList" :normalizer="normalizer" :show-count="true"
          :default-expand-level="1" placeholder="请选择" style="width: 215px;" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>

      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="clickAddbtn"
          v-hasPermi="['config:offlineChannels:add']">添加</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" :disabled="multiple" size="mini" @click="handleDelete"
          v-hasPermi="['config:offlineChannels:del']">删除</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" max-height="550" :data="dataList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handelEdit(scope.row)"
            v-hasPermi="['config:offlineChannels:edit']">编辑</el-button>
          <el-button size="mini" type="text" @click="handelStateEdit(scope.row)"
            v-hasPermi="['config:offlineChannels:status']">{{scope.row.state==0?'停用':'激活'}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="ID" prop="code" />
      <el-table-column label="名称" prop="name"></el-table-column>
      <el-table-column label="默认" prop="defaultUnderLineReceivables">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yes_no" :value="scope.row.defaultUnderLineReceivables" />
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.state" :value="scope.row.state" />
        </template>
      </el-table-column>
      <el-table-column label="资金来源" prop="sourceOfFunds">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.source_of_funds" :value="scope.row.sourceOfFunds" />
        </template>
      </el-table-column>
      <el-table-column label="创建人员" prop="createByName" />
      <el-table-column label="创建日期" prop="createTime" width="160px" />
      <el-table-column label="适用分公司" prop="companyName" />
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <el-dialog :title="ruleForm.id?'编辑线下收款渠道信息':'添加线下收款渠道信息'" :visible.sync="dialogVisible" width="600px">
      <div class="dialog-form-box">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">

          <el-form-item label="代码：" prop="code">
            <el-input v-model="ruleForm.code" style="width: 85%;"></el-input>
          </el-form-item>
          <el-form-item label="名称：" prop="name">
            <el-input v-model="ruleForm.name" style="width: 85%;"></el-input>
          </el-form-item>
          <el-form-item label="资金来源：" prop="sourceOfFunds">
            <el-select v-model="ruleForm.sourceOfFunds" placeholder="请选择" clearable style="width: 85%;">
              <el-option v-for="dict in dict.type.source_of_funds" :key="dict.value" :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="默认：" prop="defaultUnderLineReceivables">
            <el-select v-model="ruleForm.defaultUnderLineReceivables" placeholder="请选择" clearable style="width: 85%;">
              <el-option v-for="dict in dict.type.yes_no" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="状态：" prop="state">
            <el-select v-model="ruleForm.state" placeholder="请选择" clearable style="width: 85%;">
              <el-option v-for="dict in dict.type.state" :key="dict.value" :label="dict.label" :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="适用分公司：" prop="deptId">
            <treeselect v-model="ruleForm.deptId" :options="deptList" :normalizer="normalizer" :show-count="true"
              placeholder="请选择" :default-expand-level="1" style="width: 85%;" />
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handelConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import {
    offlineChannelsList,
    offlineChannelsAdd,
    offlineChannelsEdit,
    offlineChannelsDelBatch
  } from '@/api/system/dict/tradingChannels.js'
  import {
    listDept
  } from "@/api/system/dept";
  export default {
    name: "DictUnderLineReceivables",
    components: {
      Treeselect
    },
    dicts: ['state', 'source_of_funds', 'yes_no'],
    data() {
      return {
        loading: false,
        multiple: true,
        queryParams: {
          pageNum: 1,
          pageSize: 30
        },
        dialogVisible: false,
        ruleForm: {},
        rules: {
          code: [{
            required: true,
            message: "请输入说明",
            trigger: "change"
          }],
          name: [{
            required: true,
            message: "请输入说明",
            trigger: "change"
          }],
          state: [{
            required: true,
            message: "请选择",
            trigger: "change"
          }],
          defaultUnderLineReceivables: [{
            required: true,
            message: "请选择",
            trigger: "change"
          }],
          sourceOfFunds: [{
            required: true,
            message: "请选择",
            trigger: "change"
          }],
          deptId: [{
            required: true,
            message: "请选择",
            trigger: "change"
          }],
        },
        deptList: [],
        dataList: [],
        total: 0

      };
    },


    created() {
      this.getList()
      this.listDept()
    },
    methods: {
      /** 转换菜单数据结构 */
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.deptId,
          label: node.deptName,
          children: node.children
        };
      },
      //获取所属分行
      listDept() {
        listDept(this.queryParams).then(response => {
          this.deptList = this.handleTree(response.data, "deptId");
        });
      },
      getList() {
        this.loading = true;
        console.log(this.queryParams)
        offlineChannelsList(this.queryParams).then(res => {
          this.dataList = res.rows
          this.total = res.total
          this.loading = false;
        })
      },
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      handleSelectionChange(selection) {
        this.seletIds = selection.map(item => item.id)
        this.multiple = !selection.length
      },
      //批量删除产品
      handleDelete() {
        let that = this
        let ids = ''
        this.seletIds.forEach((item, i) => {
          if (this.seletIds.length - 1 == i) {
            ids += item
          } else {
            ids += item + ','
          }
        });
        this.$modal.confirm('是否确认删除所选数据项？').then(function () {
          offlineChannelsDelBatch({
            ids: ids
          }).then(res => {
            that.getList();
            that.$modal.msgSuccess("删除成功");
          });
        }).catch(function () {});
      },
      // 表单重置
      reset() {
        this.resetForm("ruleForm");
        this.ruleForm = {
          id: '',
          code: '',
          name: '',
          defaultUnderLineReceivables: this.dict.type.yes_no[0].value,
          state: this.dict.type.state[0].value,
          sourceOfFunds: this.dict.type.source_of_funds[0].value,
          deptId: null
        };
      },
      //点击添加按钮弹出表单
      clickAddbtn() {
        this.dialogVisible = true
        this.reset()
      },
      //点击编辑弹出编辑表单并赋值
      handelEdit(row) {
        this.dialogVisible = true
        this.reset()
        this.ruleForm.id = row.id
        this.ruleForm.code = row.code
        this.ruleForm.name = row.name
        this.ruleForm.defaultUnderLineReceivables = row.defaultUnderLineReceivables
        this.ruleForm.state = row.state,
          this.ruleForm.sourceOfFunds = row.sourceOfFunds,
          this.ruleForm.deptId = row.deptId
      },
      handelStateEdit(row) {
        let text = row.state == "0" ? "停用" : "激活";
        this.$modal.confirm('确认要"' + text + '"此项吗？').then(() => {
          offlineChannelsEdit({
            id: row.id,
            state: row.state == 0 ? 1 : 0
          }).then(res => {
            this.$modal.msgSuccess(text + "成功");
            this.getList();
          })
        }).catch(() => {});
      },
      //点击提交表单
      handelConfirm() {
        this.$refs["ruleForm"].validate(valid => {
          if (valid) {
            const params = {
              id: this.ruleForm.id || null,
              code: this.ruleForm.code,
              name: this.ruleForm.name,
              defaultUnderLineReceivables: this.ruleForm.defaultUnderLineReceivables,
              state: this.ruleForm.state,
              sourceOfFunds: this.ruleForm.sourceOfFunds,
              deptId: this.ruleForm.deptId
            }
            if (this.ruleForm.id) {
              offlineChannelsEdit(params).then(res => {
                this.$modal.msgSuccess("修改成功");
                this.dialogVisible = false;
                this.getList();
              })
            } else {
              offlineChannelsAdd(params).then(res => {
                this.$modal.msgSuccess("添加成功");
                this.dialogVisible = false;
                this.getList();
              })
            }
          }
        })
      }

    }
  };

</script>

<style rel="stylesheet/scss" lang="scss">

</style>
